﻿using System;
using System.Text;
using System.Web.Mvc;

public static class HtmlHelperExtensions
{
    //FckEditor
    public static string FckEditor(this HtmlHelper html, string name, string value, FckConfig objFckConfig)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<script type=\"text/javascript\">\n");
        sb.Append("\n");
        sb.Append("var oFCKeditor = new FCKeditor('" + name + "');\n");
        if (objFckConfig.Height != "100%")
        {
            sb.Append("oFCKeditor.Width = '" + objFckConfig.Width + "';\n");
        }
        if (objFckConfig.Height != "300")
        {
            sb.Append("oFCKeditor.Height = '" + objFckConfig.Height + "';\n");
        }
        if (objFckConfig.ToolbarSet == FckToolbarSet.Basic)
        {
            sb.Append("oFCKeditor.ToolbarSet = 'Basic';\n");
        }
        if (objFckConfig.Skin == FckSkin.Office2003)
        {
            sb.Append("oFCKeditor.Config['SkinPath'] = oFCKeditor.BasePath + 'editor/skins/office2003/';\n");
        }
        if (objFckConfig.Skin == FckSkin.Silver)
        {
            sb.Append("oFCKeditor.Config['SkinPath'] = oFCKeditor.BasePath + 'editor/skins/silver/';\n");
        }
        sb.Append("oFCKeditor.Value = '" + ConvertToJS(value) + "';\n");
        sb.Append("oFCKeditor.Create() ;\n");
        sb.Append("\n");
        sb.Append("</script>\n");
        return sb.ToString();
    }

    private static string ConvertToJS(string value)
    {
        return value.Replace("'", "\'").Replace("\n", "\\n").Replace("\r", "\\r");
    }


    public static string FckEditor(this HtmlHelper html, string name)
    {
        return FckEditor(html, name, Convert.ToString(html.ViewData.Eval(name)), new FckConfig { });
    }

    public static string FckEditor(this HtmlHelper html, string name, FckConfig objFckConfig)
    {
        return FckEditor(html, name, Convert.ToString(html.ViewData.Eval(name)), objFckConfig);
    }
}

public enum FckToolbarSet { Default, Basic }
public enum FckSkin { Default, Office2003, Silver }

public class FckConfig
{
    private string _width = "100%";
    private string _height = "300";
    private FckToolbarSet _toolbarSet = FckToolbarSet.Default;
    private FckSkin _skin = FckSkin.Default;

    public string Width
    {
        get { return _width; }
        set { _width = value; }
    }
    public string Height
    {
        get { return _height; }
        set { _height = value; }
    }

    public FckToolbarSet ToolbarSet
    {
        get { return _toolbarSet; }
        set { _toolbarSet = value; }
    }

    public FckSkin Skin
    {
        get { return _skin; }
        set { _skin = value; }
    }
}
